package com.alan.mvvm.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.alan.base.ui.BaseFragment;
import com.alan.mvvm.utils.Utils;


public class BaseVMFragment<VB extends ViewDataBinding, VM extends BaseViewModel> extends BaseFragment implements IBaseComponents<VB, VM> {

    protected VB mVB;
    protected VM mVM;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater,container,savedInstanceState);
        initViewBinding(inflater,container);
        initViewModel();
        initView();
        return mVB.getRoot();
    }

    protected void initViewBinding(@NonNull LayoutInflater inflater,@Nullable ViewGroup container){
        mVB = DataBindingUtil.inflate(inflater, Utils.getRes(mContext, this), container, false);
    }

    protected void initViewModel(){
        mVM = Utils.initViewModel(this, this);
        getLifecycle().addObserver(mVM);
        mVM.onCreateView();
    }

    protected void initView(){

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getLifecycle().removeObserver(mVM);
    }
}
